import { App, UpdateState } from "../App";
import { EventConst } from "../core/EventConst";

const { menu, ccclass, property } = cc._decorator;
@ccclass
@menu("自定义插件/热更新界面")
export default class HotUpdate extends cc.Component {

    @property(cc.Label)
    lblTips: cc.Label = null;
    @property(cc.Label)
    lblProgress: cc.Label = null;
    @property(cc.ProgressBar)
    progressBar: cc.ProgressBar = null;

    onLoad () {
        App.Event.on(EventConst.UPDATE_MODULE,this._update,this);
        this.progressBar.progress = 0;
        this._switchTips(App.getText('update.checking'));
    }

    onDestroy(){
        App.Util.closeLabelDot(this.lblTips);
        App.Event.offAll(this);
    }

    onClearClick(){
        App.MessageBox(App.getText('clear_cache'),(flag)=>{
            if (flag === App.MB_FLAG.OK) {
                cc.sys.isNative && jsb.fileUtils.removeDirectory(App.Native.getWritablePath());
                cc.sys.localStorage.clear();
                cc.audioEngine.stopAll();
                cc.game.restart();
            }
        });
	}

    private _switchTips(str:string){
        App.Util.closeLabelDot(this.lblTips);
        if (App.Util.isEmpty(str)) return ;
        this.lblTips.string = str;
        this.lblTips.node.active = true;
        App.Util.playLabelDot(this.lblTips);
	}

    private _update(mod:string,state:UpdateState,...arges){
        const loadAssets = ()=>{
            this._switchTips(App.getText('load_assets'));
            this.progressBar.progress = 0;
            let _id = setInterval(()=>{
                if (Math.floor(this.progressBar.progress) >= 1 && _id){
                    clearInterval(_id);
                    _id = null;
                    App.Util.closeLabelDot(this.lblTips);
                    App.Event.emit(EventConst.START_LOGIN,true);
                } else {
                    this.progressBar.progress += 0.01;
                    this.lblProgress.string = `${Math.round(this.progressBar.progress*100)}%`;
                }
            },30);
        };

        switch (state){
            case UpdateState.progress:{
                this.lblProgress.string = `${App.Util.formatByte(arges[0])}/${App.Util.formatByte(arges[1])}`;
                this.progressBar.progress = arges[2];
                break ;
            }
            case UpdateState.skip:
            case UpdateState.success:{
                this._switchTips(App.getText(state === UpdateState.success? 'update.finish': 'update.skip'));
                this.progressBar.progress = 0;
                cc.tween(this.progressBar).delay(1).call(loadAssets).start();
                break;
            }
            case UpdateState.fail:{
                !App.Util.isEmpty(arges[1]) && App.popTips(App.getText(arges[1]));
                !App.Util.isEmpty(arges[0]) && App.MessageBox(App.Util.format(App.getText('update.mianfist_error'),arges[0]),(flag:number)=>{
                    if (mod === App.MOD_NAME.MASTER){
                        flag === App.MB_FLAG.OK ? loadAssets():cc.game.end();
                    }
                });
            }
        }
    }
}
